import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:flutter_app/widget/AppBarCustom.dart';
import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:typed_data';
import 'dart:io';
import 'package:qr/qr.dart';
//import 'package:fluttertoast/fluttertoast.dart';
//import 'package:qr_utils/qr_utils.dart';
import 'package:flutter_file_manager/flutter_file_manager.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'package:flutter_app/utils/windowUtil.dart';
class QianbaoDizhi extends StatefulWidget {
  @override
  State<QianbaoDizhi> createState() => _QianbaoDizhiState();
}

class _QianbaoDizhiState extends State<QianbaoDizhi>
    with AutomaticKeepAliveClientMixin {
  GlobalKey key = new GlobalKey();
  Uint8List _QRCodeBytes;
  bool isSelectWatermark = false;
  String message = '';
  Image image;
//     Future<Image> generate() async{
//      Image image = await QrUtils.generateQR("https://blog.csdn.net/nimeghbia");
//      this.image=image;
//      setState(() {
//
//      });
//      return image;
//    }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBarCustom(
          "钱包地址",
        ),
        body: buildbody(),
//        FutureBuilder(
//          future: _files(),
//          builder: (context, snapshoot) {
//            switch (snapshoot.connectionState) {
//              case ConnectionState.none:
//                return Text('');
//              case ConnectionState.active:
//                return Text('');
//
//              case ConnectionState.done:
//                if(snapshoot.hasError){
//                  return Text('');
//                }
////                return snapshoot.data != null
////                    ? ListView.builder(
////                    itemCount: snapshoot.data.length,
////                    itemBuilder: (context, index) => Card(
////                        child: ListTile(
////                          title: Text(snapshoot.data[index].absolute.path),
////                          subtitle: Text(
////                              "Extension: ${p.extension(snapshoot.data[index].absolute.path).replaceFirst('.', '')}"), // getting extension
////                        ))) : Center(
////                  child: Text("Nothing!"),
////                );
//                return buildbody();
//              case ConnectionState.waiting:
//                return Text('');
//            }
//          },
//        ));
    );
  }

  _files() async {
    var root = await getExternalStorageDirectory();
    var fm = FileManager(root: root);
    var files =
        await fm.filesTree(excludedPaths: ["/storage/emulated/0/Android"]);
    return files;
  }

  Widget buildbody() {
   return Container(
     width: windowUtil.width,
     child: new Column(

       crossAxisAlignment: CrossAxisAlignment.center,

       children: <Widget>[
         SizedBox(height: 50.0,),
         Text("转账Bboss",style: TextStyle(fontSize: 19.0),),
        RepaintBoundary(
          key: key,
          child: new QrImage(
            data: "1234567890",
            size: 200.0,
          ),
        ),
         GestureDetector(
           child: Container(
             height: 30.0,

             child: Text(
               "保存二维码至相册",
               style: TextStyle(color: Colors.amber),
             ),
           ),
           onTap: () {
             takescreenShoot();
           },
         ),
         SizedBox(height: 50.0,),
         Text(
           "https://blog.csdn.net/nimeghbia",

         ),
         GestureDetector(
           child: Container(
             height: 30.0,
             child: Text(
               "复制地址",
               style: TextStyle(color: Colors.amber),
             ),
           ),
           onTap: () {
             Clipboard.setData(ClipboardData(text: "https://blog.csdn.net/nimeghbia"));
//             Fluttertoast.showToast(msg: "文字复制成功");
           },
         ),

       ],
     ),

   );

  }

  Widget _buildImage() {
    if (_QRCodeBytes != null) {
      return Image.memory(_QRCodeBytes);
    } else {
      return new Text('Generate an image to start',
          style: new TextStyle(fontSize: 18.0));
    }
  }

  @override
  bool get wantKeepAlive {
    return true;
  }

  takescreenShoot() async {
    int start = new DateTime.now().millisecondsSinceEpoch;
    RenderRepaintBoundary boundary = key.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    final directory = (await getApplicationDocumentsDirectory()).path;

    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngdata = byteData.buffer.asUint8List();
    print(pngdata);
    String s = '/storage/emulated/0/${DateTime.now().millisecondsSinceEpoch}.png';
    File imgFile = new File(s);
    imgFile.writeAsBytes(pngdata);
    int end = new DateTime.now().millisecondsSinceEpoch;
    print('${end - start}>');

//    Fluttertoast.showToast(msg: "保存成功$s,");
  }
}
